package com.example.sherlockzhong.plush_v2.CardIni;

import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.sherlockzhong.plush_v2.CardBeans.ConsumptionBean;
import com.example.sherlockzhong.plush_v2.Models.UserModel;
import com.example.sherlockzhong.plush_v2.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;


/*
 * Created by DosonLeung on 2014/8/13.
 */

public class ConsumeTableIni {

    public static void initConsumptionTable(JSONArray consumeArray, ConsumptionBean CB) {
        int length = consumeArray.length();
        if (length <= 0) {
            CB.setLength(1);
            CB.setContents(new String[]{"0￥"});
            CB.setHeaders(new String[]{"无"});
            CB.setDetials(new String[]{""});
            CB.setSum((double) 0);
        } else {
            CB.setLength(length);
            String headers[] = new String[length];
            String contents[] = new String[length];
            String detials[] = new String[length];
            String sum;
            double sums = 0;
            JSONObject consumeObject;
            try {
                for (int i = 0; i < length; i++) {
                    consumeObject = consumeArray.getJSONObject(i);
                    sum = consumeObject.getString("consume");
                    contents[i] = "消费额：" + sum + "(" + "余:" + consumeObject.getString("balance") + ")";
                    headers[i] = consumeObject.getString("place") + " ";
                    detials[i] = consumeObject.getString("time");
                    sums += Double.parseDouble(sum);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            CB.setHeaders(headers);
            CB.setContents(contents);
            CB.setDetials(detials);
            CB.setSum(sums);
        }
    }

    public static void generateConsumeList(Activity app, ListView otherlist, List<Map<String, Object>> otherListData, String sums) {
        String itemNames[] = {"header", "content", "detial"};
        int itemIds[] = {R.id.consume_header, R.id.consume_content, R.id.consume_detial};
        SimpleAdapter sa = new SimpleAdapter(app, otherListData, R.layout.consume_item, itemNames, itemIds);
        otherlist.setAdapter(sa);
        ((TextView) (app.findViewById(R.id.consume_sum))).setText(sums + "￥");
        ((TextView) (app.findViewById(R.id.consume_id))).setText("学号:" + UserModel.getStudentId());
    }
}
